﻿using DAL;
using FubiManger.Controls;
using Model;
using Newtonsoft.Json;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FubiManger.UI
{
    partial class AddfoodForm : Smobiler.Core.Controls.MobileForm
    {
        public AddfoodForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 商品集合
        /// </summary>
        List<ms_goods> gdlist = new List<ms_goods>();
        /// <summary>
        /// 购物车
        /// </summary>
        public List<Gdpro> carlist
        {
            get; set;
        }
        /// <summary>
        /// 订单号
        /// </summary>
        public string ordersn { get; set; }
        /// <summary>
        /// 订单金额
        /// </summary>
        private double orderprice = 0;
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddfoodForm_Load(object sender, EventArgs e)
        {
            List<ms_classify> clist = classifyDal.Querybymerid(merchantid);//商家ID
            for (int i = 0; i < clist.Count; i++)
            {
                businessmenucontrol con1 = new businessmenucontrol();
                con1.businname = clist[i].title;
                con1.imgnmae = "";
                con1.clsid = clist[i].id;
                con1.Controls.Find("btnvalue").BackColor = System.Drawing.Color.Transparent;
                Button btn = (Button)con1.Controls.Find("btnvalue");
                btn.ForeColor = System.Drawing.Color.DimGray;
                panmenu.Controls.Add(con1);
            }
        }

        /// <summary>
        /// 移除背景样式
        /// </summary>
        /// <param name="bt"></param>
        private void RemoveImgStyle(Image img)
        {

            img.ResourceID = "";
        }

        /// <summary>
        /// 移除全部按钮的样式
        /// </summary>
        public void RemoveAllImgStyle()
        {
            foreach (var con in panmenu.Controls)
            {
                Image img = (Image)((MobileUserControl)con).Controls.Find("imgmenu", false);
                Button btn = (Button)((MobileUserControl)con).Controls.Find("btnvalue", false);
                btn.ForeColor = System.Drawing.Color.DimGray;
                RemoveImgStyle((Image)img);

            }
        }
        /// <summary>
        /// 修改按钮样式
        /// </summary>
        /// <param name="button"></param>
        public void ChangeImgStyle(Image img)
        {

            img.ResourceID = "busin_menu";
        }
        /// <summary>
        /// 根据商家菜单改变内容
        /// </summary>
        /// <param name="name"></param>
        public void Show_name(string name, string clsid)
        {
            if (panpro.Controls.Count > 0)
                panpro.Controls.Clear();
            gdlist = goodsDal.Querybyclassid(clsid, merchantid);//通过分类ID查线上商品列表
            panpro.Height = gdlist.Count * 82;
            for (int i = 0; i < gdlist.Count; i++)
            {
                Prodouctcontrol pcon = new Prodouctcontrol();
                pcon.cover = gdlist[i].cover;//封面
                pcon.describes = gdlist[i].describes;//描述
                pcon.gdname = gdlist[i].goods_name;//商品名称
                pcon.price = Convert.ToDouble(gdlist[i].price);//销售价
                pcon.market_price = "￥" + gdlist[i].market_price;//市场价
                pcon.gdid = gdlist[i].id;//商品ID
                pcon.stype = "3";//加单                     
                panpro.Controls.Add(pcon);
            }

        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="count"></param>
        /// <param name="img"></param>
        public void Addpro(string name, string cover, double money, int num)
        {
            Gdpro pro = new Gdpro();
            pro.proimg = cover;
            pro.proname = name;
            pro.price = money;
            pro.count = num;
            int index = carlist.FindIndex(x => x.proname == name);
            if (index == -1)
                carlist.Add(pro);
            else
            {
                carlist[index].count++;
            }

            if (carlist.Count > 0)
            {
                int count = 0;
                double price = 0;
                for (int i = 0; i < carlist.Count; i++)
                {
                    count += carlist[i].count;
                    price += (carlist[i].price * carlist[i].count);
                }
                orderprice = price;
            }
            
        }
        /// <summary>
        /// 更新购物车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnsub_Press(object sender, EventArgs e)
        {
            string goodsinfo = JsonConvert.SerializeObject(carlist);
            orderDal.Updategoods_info(ordersn, goodsinfo,orderprice.ToString());//更新订单中的购物商品
            this.Close();
        }
    }
}